package com.itjavas.controller;

import com.itjavas.domain.Account;
import com.itjavas.service.AccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService  accountService;
    @RequestMapping("/findAllAccounts")
    public String findAllAccount(Model model){
        System.out.println("springMVC的环境搭建");
        List< Account >accountList=accountService.findAllAccounts();
//        model.addAttribute("accounts","123321");
        model.addAttribute("accountList",accountList);
        return "listAccounts";
    }
    @RequestMapping("/addUI")
    public String addUI(){//页面跳转
        return "addAccount";
    }
    @RequestMapping("/addAccount")
    public String addAccount(Account account){
        accountService.addAccount(account);
        return "redirect:/account/findAllAccounts";
    }
    @RequestMapping("/editUI")
    public String editUI(Integer id,Model model){
        //根据数据库中的id修改数据
        Account account=accountService.findAllAccountById(id);
         model.addAttribute("account",account);
         return "editAccount";
    }
    @RequestMapping("/upAccount")
    public String upAccount(Account account){
        accountService.upAccount(account);
        return "redirect:/account/findAllAccounts";
    }

    @RequestMapping("/login")
    public String login(Account account){
        Account account1=accountService.findAllAccountById(account.getId());
        if(StringUtils.equals(account1.getName(),account.getName())){
            return "redirect:/account/findAllAccounts";
        } else
            return "error";
        }
     @RequestMapping("register")
    public String register(){return "addAccount";
     }
}
